#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @author: zhangbo

# @time: 2025/3/4 17:40

import os.path

import yaml


class Read_yaml:

    def __init__(self, yaml_path_list: list[str] = None):
        """
        加载yaml
        :param yaml_path_list:
        """
        # 定义参数接收字典
        self.config_data: dict[str, dict[str, any]] = {}

        if yaml_path_list:
            for yaml_path in yaml_path_list:
                with open(yaml_path, "r+", encoding="utf-8") as file:
                    # 读取yaml文件中的全部内容
                    config_data = yaml.safe_load(file)
                    if config_data:
                        # 取当前文件名作为 存储key值
                        self.config_data[os.path.splitext(os.path.basename(yaml_path))[0]] = config_data

    def get_config_data(self, config_name: str):
        """
        读取yaml
        :param config_name:
        :return:
        """
        config = self.config_data.get(config_name)
        if config is None:
            # 没有找到目标key值，抛出异常
            raise KeyError(f"Configuration '{config_name}' not found in config data.")
        else:
            return config

